//
//  MKAPIPreCompile.h
//  MKAPI
//
//  Created by 苏尚进 on 2022/6/9.
//  Copyright © 2022 moon. All rights reserved.
//


/**
 *  定义单例
 *
 *  @param ...单例名称,此处只是作为注释,无实际使用
 *
 */
#undef	MKAPIAS_SINGLETON
#define MKAPIAS_SINGLETON( ... ) \
- (nonnull instancetype)sharedInstance; \
+ (nonnull instancetype)sharedInstance;

#undef	MKAPIDEF_SINGLETON
#define MKAPIDEF_SINGLETON( ... ) \
- (instancetype)sharedInstance{ \
	return [[self class] sharedInstance]; \
} \
+ (instancetype)sharedInstance{ \
	static dispatch_once_t once; \
	static id __singleton__; \
	dispatch_once( &once, ^{ __singleton__ = [[self alloc] init]; } ); \
	return __singleton__; \
}

#undef	MKAPIDEF_SINGLETON_SUBCLASS
#define MKAPIDEF_SINGLETON_SUBCLASS \
- (instancetype)sharedInstance{ \
	return [[self class] sharedInstance]; \
} \
+ (instancetype)sharedInstance{ \
	static dispatch_once_t once;\
    static NSMutableDictionary * __singleton__;\
    dispatch_once( &once, ^{ __singleton__ = [[NSMutableDictionary alloc] init]; } );\
    id shareObject;\
    @synchronized (__singleton__) {\
       shareObject = __singleton__[NSStringFromClass(self)];\
        if(!shareObject){\
            shareObject = [[self alloc] init];\
            __singleton__[NSStringFromClass(self)] = shareObject;\
        }\
    }\
    return shareObject;\
}

/**
 *  对于block外部与内部间的变量传递,定义block外的weak弱引用与block内的strong强引用,防止因block而引起的循环引用内存泄露
 *	范例:
 - (void)test{
	NSObject *obj;
	@MKAPI_WEAKIFY(self);
	@MKAPI_WEAKIFY(obj);
	void(^testBlock)() = ^(){
		@MKAPI_NORMALIZE(self);
		@MKAPI_NORMALIZE(obj);
		...
	};
 }
 *  @param objc_arc 对象
 *
 */
//由于以前的weakify,normalize,normalizeAndNoNil会有命名冲突问题,因此修改这些宏,添加MK前缀,并大写化
#undef MKAPI_WEAKIFY
#define MKAPI_WEAKIFY( x )	autoreleasepool{} __weak __typeof__(x) __weak_##x##__ = x;

#undef	MKAPI_NORMALIZE
#define MKAPI_NORMALIZE( x )	try{} @finally{} __typeof__(x) x = __weak_##x##__;
#undef	MKAPI_NORMALIZEANDNOTNIL
#define MKAPI_NORMALIZEANDNOTNIL( x )	try{} @finally{} __typeof__(x) x = __weak_##x##__;if(x==nil)return;
